<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Donation Controller.
 * 
 * @class     Donation
 * @author    Iqbal
 * @version   1.0.0
 * @copyright Copyright (c) 2014 IPPI (http://www.ippi.or.id)
 */
class donation extends CI_Controller {
    /**
     * Member Logged
     * 
     * @var object $member_logged
     */
    public $member_logged;
    
    /**
	 * Constructor.
	 */
    function __construct() {
        parent::__construct();
        
        // Load model
        $this->load->model('model_donation', 'm_donation', TRUE);
        
        // Check is member logged in
        is_member_logged_in();
        
        // Set member logged
        $this->member_logged        = ippi_get_current_member();
    }
    
    /**
	 * Insert Donation function.
     * @return AJAX String
	 */
    function insert(){        
        // Set current date for datecreated and datemodified
        $curdate        = date("Y-m-d H:i:s");
        
        // Set donation data
        $donation_data  = array(
            "id_member"         => $this->member_logged->id_member,
            "kategori"          => ippi_isset($this->input->post("donasi_kat"), ''),
            "jumlah_donasi"     => (int) ippi_isset($this->input->post("donasi_jum"), ''),
            "keterangan"        => ippi_isset($this->input->post("donasi_ket"), ''),
            'datecreated'       => $curdate,
            'datemodified'      => $curdate,
        );
        
        // Save donation data
        $save = $this->m_donation->insert_donation($donation_data);
        
        // Response of save donation data
        if( $save ) { 
            $donasi             = $this->m_donation->get_donation($save);
            echo json_encode($donasi);
        }else{ 
            echo "0"; 
        }
    }
    
    /**
	 * Confirmation Donation function.
     * @return AJAX String
	 */
    function confirmation(){ 
        // Set current date for datecreated and datemodified
        $curdate        = date("Y-m-d H:i:s");
        $id_donasi      = ippi_isset($this->input->post("don_id"), '');
        
        // Set confirmation donation data
        $confirm_data   = array(
            'id_donasi'     => $id_donasi,
            'id_member'     => $this->member_logged->id_member,
            'kategori'      => ippi_isset($this->input->post("donasi_conf_kat"), ''),
            'jumlah_donasi' => ippi_isset($this->input->post("donasi_conf_jum"), ''),
            'tgl_donasi'    => date( "Y-m-d", strtotime( ippi_isset($this->input->post("donasi_conf_tgl"), '') )),
            'cara_donasi'   => ippi_isset($this->input->post("donasi_conf_cara"), ''),
            'bank_asal'     => ippi_isset($this->input->post("donasi_conf_bankasal"), ''),
            'rekening_asal' => ippi_isset($this->input->post("donasi_conf_rekasal"), ''),
            'bank_tujuan'   => ippi_isset($this->input->post("donasi_conf_brtuj"), ''),
            'keterangan'    => ippi_isset($this->input->post("donasi_conf_ket"), ''),
            'datecreated'   => $curdate,
            'datemodified'  => $curdate
        );
        
        // Save confirmation donation
        $save = $this->m_donation->insert_conf_donation($confirm_data);
        
        // Response confirmation donation
        if( $save ){
            $data_update_donasi = array(
                'status'        => 1,
                'datemodified'  => $curdate
            );
            $this->m_donation->update_donation($data_update_donasi, $id_donasi);
            echo '1';
        }else{
            echo '0';
        }
    }
    
    /**
	 * Load Donation function.
     * 
     * @param[in] int $offset Offset of Table Donation data
     * 
     * @return AJAX String
	 */
    function load_donation($offset=0){
        // Set variables
        $limit          = get_option('donation_limit');
        $condition      = '';
        
        // Search nominal
        $search_val     = ippi_isset($this->input->get('search_val'), '');
        if( !empty($search_val) ) {
            $f          = ' AND %jumlah_donasi% = %s%';
            $condition .= str_replace('%s%', $search_val, $f);
        }
        
        // Search date
        $search_date    = trim( ippi_isset($this->input->get('search_tanggal'), '') );
        if( !empty($search_date) ) {
            $f          = ' AND %datemodified% LIKE "%s%%"';
            $condition .= str_replace('%s%', $search_date, $f);
        }
        
        // Search status
        $search_status  = trim( ippi_isset($this->input->get('search_status'), '') );
        if( $search_status != '' ) {
            $f          = ' AND %status% = %s%';
            $condition .= str_replace('%s%', $search_status, $f);
        }
        
        // Search kategori
        $search_kat     = trim( ippi_isset($this->input->get('search_kategori'), '') );
        if( $search_kat != '' ) {
            $f          = ' AND %kategori% = "%s%"';
            $condition .= str_replace('%s%', $search_kat, $f);
        }
        
        // Get donation data
        $donation_arr   = $this->m_donation->get_all_donation_data($this->member_logged->id_member, $limit, $offset, $condition);
        $donation       = '';
        $pagination     = '';
        
        // Check for response get donation data
        if( !empty($donation_arr) ){
            $total      = ippi_get_last_found_rows();
            
            foreach($donation_arr as $don){
                $donation_date  = date('Y-m-d', strtotime($don->datecreated));
                
                if($don->status == 0)       { $stat_css = "alert-warning"; $stat = 'Pending'; }
                elseif($don->status == 1)   { $stat_css = "alert-success"; $stat = 'Confirmed'; }
                elseif($don->status == 2)   { $stat_css = "alert-info"; $stat = 'Processed'; }
                
                $donation   .= '
                <li>
                    <span class="btn btn-primary btn-sm don-tgl">Tanggal : '.$donation_date.'</span>
                    <span class="don-stat '.$stat_css.'">' . $stat . '</span>
                    <div class="don-currency">Rp <span style="float: right;">' .number_format($don->jumlah_donasi, 2, ',', '.'). '</span></div>';
                    
                    if($don->status == 1 || $don->status == 2){
                        $donation  .= '
                        <strong>Confirm</strong> | <a href="#" class="btn-det" id="'.$don->id_donasi.'"><strong><abbr>Details</abbr></strong></a>
                        <div class="well don-details" id="don-det-'.$don->id_donasi.'">';
                            $don_dt         = $this->m_donation->get_details_donation($don->id_donasi);
                                
                            if( !empty($don_dt) ){
                                if($don_dt->kategori == 'female')                   { $kat  = 'Female Fund'; }
                                elseif($don_dt->kategori == 'baby')                 { $kat  = 'Baby Fund'; }
                                elseif($don_dt->kategori == 'children')             { $kat  = 'Children Fund'; }
                                
                                if($don_dt->cara_donasi == 'cash')                  { $cara = 'Tunai (Cash)'; }
                                elseif($don_dt->cara_donasi == 'transfer')          { $cara = 'Transfer'; }
                                elseif($don_dt->cara_donasi == 'mobile-banking')    { $cara = 'Mobile Banking'; }
                                elseif($don_dt->cara_donasi == 'internet-banking')  { $cara = 'Internet Banking'; }
                                
                                if($don_dt->bank_tujuan == 'bca')                   { $bank = 'BCA (987654321)'; }
                                elseif($don_dt->bank_tujuan == "mandiri")           { $bank = 'Mandiri (900-123456-435)'; }
                                elseif($don_dt->bank_tujuan == "bni")               { $bank = 'BNI (787738-3453)'; }
                                elseif($don_dt->bank_tujuan == "bri")               { $bank = 'BRI (8774933-48)'; }
                                elseif($don_dt->bank_tujuan == "cimb")              { $bank = 'CIMB (4444-3443-34)'; }
                                
                                $donation  .= '
                                <dl>
                                <dt>Kategoru</dt><dd>'.$kat.'</dd>
                                <dt>Cara Donasi</dt><dd>'.$cara.'</dd>
                                <dt>Bank Asal</dt><dd>'.strtoupper($don_dt->bank_asal).'</dd>
                                <dt>Rekening Asal</dt><dd>'.$don_dt->rekening_asal.'</dd>
                                <dt>Bank dan Rekening Tujuan</dt><dd>'.$bank.'</dd>
                                <dt>Keterangan</dt><dd>'.$don_dt->keterangan.'</dd>
                                </dl>';
                            }else{
                                $donation  .= '<strong>Donasi ini tidak memiliki details</strong>';
                            }
                        $donation  .= '</div>';
                    }else{
                        $donation  .= '<a href="#" class="btn-conf" id="'.$don->id_donasi.'"><strong><abbr>Confirm</abbr></strong></a> | <strong>Details</strong>';
                    }
                $donation   .= '</li>';
            }
            
            // Set Pagination
            if ($total > $limit){
                $this->load->library('pagination');
                
                $config['base_url']     = 'donation/load_donation/';
                $config['total_rows']   = $total;
                $config['per_page']     = $limit; 
                $config['anchor_class'] = 'class="pagination_donation" ';
                
                $this->pagination->initialize($config);
                $pagination             = $this->pagination->create_links(); 
            }
        }else{
            $donation  .= '<li class="no-don" style="margin-bottom: none;">Tidak ada donasi</li>';
        }
        
        // Set JSON data
        $data = array(
            'donation'      => $donation,
            'pagination'    => $pagination
        );
        
        // JSON encode data
        die(json_encode($data));
    }
    
    /**
	 * Get Donation by Donation ID function. 
     * @return AJAX String
	 */
    function get_donation(){
        // Set donation id param
        $id_donasi      = ippi_isset($this->input->post("id_donasi"), '');
        
        // Get donation
        $donasi         = $this->m_donation->get_donation($id_donasi);
        $donasi         = (array) $donasi;
        
        // Response of get donation
        if( is_array($donasi) && !empty($donasi) ){
            echo json_encode($donasi);
        }else{
            echo "0";
        }
    }
}

/* End of file donation.php */
/* Location: ./application/controllers/donation.php */